package com.modifiertech.lineage.client.gui.ftbint;

import com.feed_the_beast.ftblib.lib.gui.*;
import com.feed_the_beast.ftblib.lib.icon.Color4I;
import com.feed_the_beast.ftblib.lib.util.misc.MouseButton;
import com.modifiertech.lineage.BaseConst;
import com.modifiertech.lineage.Lineage;
import com.modifiertech.lineage.api.PlayerData;
import com.modifiertech.lineage.event.PlayerDataHandler;
import com.modifiertech.lineage.network.MessageAttributeModify;
import com.modifiertech.lineage.network.NetworkHandler;
import scala.actors.threadpool.ExecutorCompletionService;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PanelAttrV2 extends Panel {
    private GuiLineageV2 mainGui;
    private TextField textFieldStrength;
    private TextField textFieldStrengthValue;
    private Button strengthPlus,strengthSubs;//+-
    public ConcurrentHashMap<String,Integer> attrModifierMap=new ConcurrentHashMap<>();
    public volatile int attrUndistribute=0;
    private TextField textFieldAgile;
    private TextField textFieldAgileValue;
    private Button agilePlus,agileSubs;//+-
    private TextField textFieldIntelligence;
    private TextField textFieldIntelligenceValue;
    private Button intelligencePlus,intelligenceSubs;//+-
    private TextField textFieldPhysique;
    private TextField textFieldPhysiqueValue;
    private Button physiquePlus,physiqueSubs;//+-
    private Button accept,reset;
    private TextField attrLeftText ,attrLeftTextValue;
    public PlayerData playerDataRelClient;
    //


    public void initialize() {
        initPlayerData();
        initAttrString();
        initAttrOptBtn();
    }

    private void initPlayerData() {
        this.playerDataRelClient =PlayerDataHandler.get(Lineage.proxy.getClientPlayer());
    }


    public void initAttrOptBtn(){


    }

    /**初始化临时属性操作map
     *
     */
    public void initTempAttrMap(){
        for (BaseConst.Attr value : BaseConst.Attr.values()) {
            if (value.isPlayerAttr()){
                attrModifierMap.put(value.name(),0);
            }
        }
    }

    public void initAttrString() {
        PlayerData data = PlayerDataHandler.get(Lineage.proxy.getClientPlayer());
        initTempAttrMap();
//
//        attrModifierMap.put(BaseConst.Attr.PLAYER_ADV_TAG__ATTR_STRENGTH.getName(),0);
//        attrModifierMap.put(BaseConst.Attr.PLAYER_ADV_TAG__ATTR_AGILE.getName(),0);
//        attrModifierMap.put(BaseConst.Attr.PLAYER_ADV_TAG__ATTR_INTELLIGENCE.getName(),0);
//        attrModifierMap.put(BaseConst.Attr.PLAYER_ADV_TAG__ATTR_PHYSIQUE.getName(),0);

        //
        attrLeftText=new TextField(this);
        attrLeftText.setPosAndSize(5,5,20,5);
        attrLeftText.setText("§lAttrPoints");
        attrLeftTextValue=new TextField(this);
        attrLeftTextValue.setPosAndSize(70,5,20,5);
        attrLeftTextValue.setText(data.getAttr(BaseConst.Attr.UnDistribute.name())+"");


        //
        textFieldStrength=new TextField(this);
        textFieldStrength.setPosAndSize(5,15,20,5);
        textFieldStrength.setText("Strength:");
        textFieldStrengthValue=new TextField(this);
        textFieldStrengthValue.setPosAndSize(70,15,10,5);
        textFieldStrengthValue.setText(data.getAttr(BaseConst.Attr.Strength.name())+"");
        strengthPlus=new BtnAttr(this,BtnAttr.ATTR_ADD,BaseConst.Attr.Strength.name());
        strengthPlus.setPosAndSize(90,15,5,5);
        strengthSubs=new BtnAttr(this,BtnAttr.ATTR_SUB,BaseConst.Attr.Strength.name());
        strengthSubs.setPosAndSize(100,15,5,5);
        accept=new BtnSure(this);
//        accept.setPosAndSize(150,150,15,15);

        //
        textFieldAgile=new TextField(this);
        textFieldAgile.setPosAndSize(5,25,20,5);
        textFieldAgile.setText("Agile:");
        textFieldAgileValue=new TextField(this);
        textFieldAgileValue.setPosAndSize(70,25,10,5);
        textFieldAgileValue.setText(data.getAttr(BaseConst.Attr.Agile.name())+"");
        agilePlus=new BtnAttr(this,BtnAttr.ATTR_ADD,BaseConst.Attr.Agile.name());
        agilePlus.setPosAndSize(90,25,5,5);
        agileSubs=new BtnAttr(this,BtnAttr.ATTR_SUB,BaseConst.Attr.Agile.name());
        agileSubs.setPosAndSize(100,25,5,5);
        //
        textFieldIntelligence=new TextField(this);
        textFieldIntelligence.setPosAndSize(5,35,20,5);
        textFieldIntelligence.setText("Intelligence:");
        textFieldIntelligenceValue=new TextField(this);
        textFieldIntelligenceValue.setPosAndSize(70,35,10,5);
        textFieldIntelligenceValue.setText(data.getAttr(BaseConst.Attr.Intelligence.name())+"");
        intelligencePlus=new BtnAttr(this,BtnAttr.ATTR_ADD,BaseConst.Attr.Intelligence.name());
        intelligencePlus.setPosAndSize(90,35,5,5);
        intelligenceSubs=new BtnAttr(this,BtnAttr.ATTR_SUB,BaseConst.Attr.Intelligence.name());
        intelligenceSubs.setPosAndSize(100,35,5,5);
        //
        textFieldPhysique=new TextField(this);
        textFieldPhysique.setPosAndSize(5,45,20,5);
        textFieldPhysique.setText("Physique:");
        textFieldPhysiqueValue=new TextField(this);
        textFieldPhysiqueValue.setPosAndSize(70,45,10,5);
        textFieldPhysiqueValue.setText(data.getAttr(BaseConst.Attr.Physique.name())+"");
        physiquePlus=new BtnAttr(this,BtnAttr.ATTR_ADD,BaseConst.Attr.Physique.name());
        physiquePlus.setPosAndSize(90,45,5,5);
        physiqueSubs=new BtnAttr(this,BtnAttr.ATTR_SUB,BaseConst.Attr.Physique.name());
        physiqueSubs.setPosAndSize(100,45,5,5);
        //
    }


    public PanelAttrV2(Panel panel) {
        super(panel);
        initialize();
    }
    @Override
    public void addWidgets() {
        add(attrLeftText);
        add(attrLeftTextValue);

        //
        add(textFieldStrength);
        add(textFieldAgile);
        add(textFieldIntelligence);
        add(textFieldPhysique);
        //
        add(textFieldStrengthValue);
        add(textFieldAgileValue);
        add(textFieldIntelligenceValue);
        add(textFieldPhysiqueValue);
        //
        add(strengthPlus);
        add(strengthSubs);

        add(agilePlus);
        add(agileSubs);

        add(intelligencePlus);
        add(intelligenceSubs);

        add(physiquePlus);
        add(physiqueSubs);
        //
        add(accept);


    }

    @Override
    public void tick() {
        super.tick();
        //render attribute
        renderAttribute();
    }

    private void renderAttribute() {
        PlayerData data = PlayerDataHandler.get(Lineage.proxy.getClientPlayer());
        if(null!=data){
            //LEFT POINT SHOW
            int show=data.getAttr(BaseConst.Attr.UnDistribute.name())-getModifyAttrTotal();//show point
            //if showpoint >0 then use targetColor
            if(show>0){
                this.attrLeftTextValue.setText("§6"+show);
            }else{
                this.attrLeftTextValue.setText(show+"");
            }

            //STRENGTH SHOW
            if(attrModifierMap.get(BaseConst.Attr.Strength.name())>0){
                this.getTextFieldStrengthValue().setText("§a"+(data.getAttr(BaseConst.Attr.Strength.name())+attrModifierMap.get(BaseConst.Attr.Strength.name())));
            }else if (attrModifierMap.get(BaseConst.Attr.Strength.name())<0){
                this.getTextFieldStrengthValue().setText("§4"+(data.getAttr(BaseConst.Attr.Strength.name())+attrModifierMap.get(BaseConst.Attr.Strength.name())));
            }else{
                this.getTextFieldStrengthValue().setText(data.getAttr(BaseConst.Attr.Strength.name())+"");
            }
            //AGLIE SHOW
            if(attrModifierMap.get(BaseConst.Attr.Agile.name())>0){
                this.getTextFieldAgileValue().setText("§a"+(data.getAttr(BaseConst.Attr.Agile.name())+attrModifierMap.get(BaseConst.Attr.Agile.name())));
            }else if (attrModifierMap.get(BaseConst.Attr.Agile.name())<0){
                this.getTextFieldAgileValue().setText("§4"+(data.getAttr(BaseConst.Attr.Agile.name())+attrModifierMap.get(BaseConst.Attr.Agile.name())));
            }else{
                this.getTextFieldAgileValue().setText(data.getAttr(BaseConst.Attr.Agile.name())+"");
            }
            //INTELLIGENCE SHOW
            if(attrModifierMap.get(BaseConst.Attr.Intelligence.name())>0){
                this.getTextFieldIntelligenceValue().setText("§a"+(data.getAttr(BaseConst.Attr.Intelligence.name())+attrModifierMap.get(BaseConst.Attr.Intelligence.name())));
            }else if (attrModifierMap.get(BaseConst.Attr.Intelligence.name())<0){
                this.getTextFieldIntelligenceValue().setText("§4"+(data.getAttr(BaseConst.Attr.Intelligence.name())+attrModifierMap.get(BaseConst.Attr.Intelligence.name())));
            }else{
                this.getTextFieldIntelligenceValue().setText(data.getAttr(BaseConst.Attr.Intelligence.name())+"");
            }
            //PHYSIQUE SHOW
            if(attrModifierMap.get(BaseConst.Attr.Physique.name())>0){
                this.getTextFieldPhysiqueValue().setText("§a"+(data.getAttr(BaseConst.Attr.Physique.name())+attrModifierMap.get(BaseConst.Attr.Physique.name())));
            }else if (attrModifierMap.get(BaseConst.Attr.Physique.name())<0){
                this.getTextFieldPhysiqueValue().setText("§4"+(data.getAttr(BaseConst.Attr.Physique.name())+attrModifierMap.get(BaseConst.Attr.Physique.name())));
            }else{
                this.getTextFieldPhysiqueValue().setText(data.getAttr(BaseConst.Attr.Physique.name())+"");
            }
        }
        //BTN ACCEPT SHOW
        accept.setPosAndSize(width-30,height-30,25,25);

    }


    @Override
    public void alignWidgets() {
//        if(mainGui.width>320&& mainGui.width<=640){
//            float scale=0.75f;
//            textFieldStrength.setScale(scale);
//             textFieldStrengthValue.setScale(scale);
//             textFieldAgile.setScale(scale);
//            textFieldAgileValue.setScale(scale);
//             textFieldIntelligence.setScale(scale);
//             textFieldIntelligenceValue.setScale(scale);
//             textFieldPhysique.setScale(scale);
//             textFieldPhysiqueValue.setScale(scale);
//             attrLeftText.setScale(scale);
//             attrLeftTextValue.setScale(scale);
//        }else{
//        }

    }

    @Override
    public void drawBackground(Theme theme, int x, int y, int w, int h) {
        Color4I.LIGHT_RED.withAlpha(30).withBorder(Color4I.BLACK,true).draw(x, y, w, h);
//        theme.drawPanelBackground(x, y, w, h);
    }

    public TextField getTextFieldStrength() {
        return textFieldStrength;
    }

    public void setTextFieldStrength(TextField textFieldStrength) {
        this.textFieldStrength = textFieldStrength;
    }

    public TextField getTextFieldAgile() {
        return textFieldAgile;
    }

    public void setTextFieldAgile(TextField textFieldAgile) {
        this.textFieldAgile = textFieldAgile;
    }

    public TextField getTextFieldIntelligence() {
        return textFieldIntelligence;
    }

    public void setTextFieldIntelligence(TextField textFieldIntelligence) {
        this.textFieldIntelligence = textFieldIntelligence;
    }

    public TextField getTextFieldPhysique() {
        return textFieldPhysique;
    }

    public void setTextFieldPhysique(TextField textFieldPhysique) {
        this.textFieldPhysique = textFieldPhysique;
    }

    public Button getStrengthPlus() {
        return strengthPlus;
    }

    public void setStrengthPlus(Button strengthPlus) {
        this.strengthPlus = strengthPlus;
    }

    public Button getStrengthSubs() {
        return strengthSubs;
    }

    public void setStrengthSubs(Button strengthSubs) {
        this.strengthSubs = strengthSubs;
    }

    public Button getAgilePlus() {
        return agilePlus;
    }

    public void setAgilePlus(Button agilePlus) {
        this.agilePlus = agilePlus;
    }

    public Button getAgileSubs() {
        return agileSubs;
    }

    public void setAgileSubs(Button agileSubs) {
        this.agileSubs = agileSubs;
    }

    public Button getIntelligencePlus() {
        return intelligencePlus;
    }

    public void setIntelligencePlus(Button intelligencePlus) {
        this.intelligencePlus = intelligencePlus;
    }

    public Button getIntelligenceSubs() {
        return intelligenceSubs;
    }

    public void setIntelligenceSubs(Button intelligenceSubs) {
        this.intelligenceSubs = intelligenceSubs;
    }

    public Button getPhysiquePlus() {
        return physiquePlus;
    }

    public void setPhysiquePlus(Button physiquePlus) {
        this.physiquePlus = physiquePlus;
    }

    public Button getPhysiqueSubs() {
        return physiqueSubs;
    }

    public void setPhysiqueSubs(Button physiqueSubs) {
        this.physiqueSubs = physiqueSubs;
    }

    public TextField getTextFieldStrengthValue() {
        return textFieldStrengthValue;
    }

    public void setTextFieldStrengthValue(TextField textFieldStrengthValue) {
        this.textFieldStrengthValue = textFieldStrengthValue;
    }

    public TextField getTextFieldAgileValue() {
        return textFieldAgileValue;
    }

    public void setTextFieldAgileValue(TextField textFieldAgileValue) {
        this.textFieldAgileValue = textFieldAgileValue;
    }

    public TextField getTextFieldIntelligenceValue() {
        return textFieldIntelligenceValue;
    }

    public void setTextFieldIntelligenceValue(TextField textFieldIntelligenceValue) {
        this.textFieldIntelligenceValue = textFieldIntelligenceValue;
    }

    public TextField getTextFieldPhysiqueValue() {
        return textFieldPhysiqueValue;
    }

    public void setTextFieldPhysiqueValue(TextField textFieldPhysiqueValue) {
        this.textFieldPhysiqueValue = textFieldPhysiqueValue;
    }


    public static class BtnAttr extends Button{
        PanelAttrV2 rel;
        public static final int ATTR_ADD=1;
        public static final int ATTR_SUB=0;
        private int optType=0;  //0 is sub /1 is add
        private String attrType="-";
        public volatile boolean isActive=false;
        static final List<String> ATTRTYPES=new ArrayList<String>(Arrays.asList(
                new String[]{BaseConst.Attr.Strength.name(),BaseConst.Attr.Agile.name(),BaseConst.Attr.Intelligence.name(),BaseConst.Attr.Physique.name()}));

        public BtnAttr(PanelAttrV2 panel,int optType,String attrType) {
            super(panel);
            this.rel=panel;
            this.optType=optType;
            this.attrType=attrType;
            if(0==optType){
                this.setIcon(GuiIconsV2.SUB);
            }
            if (1==optType){
                this.setIcon(GuiIconsV2.ADD);
            }
        }
        @Override
        public void onClicked(MouseButton button) {
            if ("-".equals(this.attrType))return;

            if(!ATTRTYPES.contains(attrType))return;
            int  attrvalue=1;
            PlayerData data = PlayerDataHandler.get(Lineage.proxy.getClientPlayer());




//            String optTypeInner=0==optType?BaseConst.PLAYER_BASE_OPT_SUB:1==optType?BaseConst.PLAYER_BASE_OPT_ADD:"-";
            int optValue=0==optType?0-attrvalue:1==optType?attrvalue:0;
            if(rel.attrModifierMap.get(attrType)+optValue<0|| rel.attrUndistribute+optValue>data.getAttr(BaseConst.Attr.UnDistribute.name())){
                return;
            }else{
                rel.attrModifierMap.put(attrType,rel.attrModifierMap.get(attrType)+optValue);
                rel.attrUndistribute+=optValue;
            }
        }
        @Override
        public boolean mousePressed(MouseButton button) {
            if (isMouseOver())
            {
                if (getWidgetType() != WidgetType.DISABLED)
                {
                    ExecutorService executorService= Executors.newSingleThreadExecutor();
                    isActive=true;
                    onClicked(button);
                    executorService.submit(()->{
                        int i=0;
                        while (true){
                            long sleepTime=i<=5?200:
                                    50;
                            try {
                                Thread.sleep(sleepTime);
                                i++;
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            } if (!isActive)break;
                            onClicked(button);
                        }
                    });
                }
                return true;
            }
            return false;
        }

        @Override
        public void mouseReleased(MouseButton button) {
            super.mouseReleased(button);
            isActive=false;
        }

        @Override
        public boolean shouldDraw() {
            if (0==optType){
                if(rel.attrModifierMap.get(attrType)>0)return true;
                return false;
            }else if (1==optType){
                if (rel.playerDataRelClient.getAttr(BaseConst.Attr.UnDistribute.name())>0){
                    return true;
                }else{
                    return false;
                }
            }else{ }
            return true;
        }
    }

    public static class BtnSure extends SimpleButton{
        PanelAttrV2 rel;
        public BtnSure(PanelAttrV2 panel) {
            super(panel, "确认", GuiIconsV2.ACCEPT, (h,v)->{});
            this.rel=panel;
        }

        /**点击后进行服务端通讯,服务端进行数据修改并同步至客户端
         *
         * @param button
         */
        @Override
        public void onClicked(MouseButton button) {
            MessageAttributeModify messageAttributeModify=new MessageAttributeModify();
            for (Map.Entry<String,Integer> entry:rel.attrModifierMap.entrySet()){
                if(entry.getValue()>0){
                    //累加
                    messageAttributeModify.write(BaseConst.Attr.valueOf(entry.getKey()), BaseConst.AttrOperate.add,entry.getValue());
                    //累减
                    messageAttributeModify.write(BaseConst.Attr.UnDistribute, BaseConst.AttrOperate.sub,entry.getValue());
                }
            }
            //通讯
            NetworkHandler.INSTANCE.sendToServer(messageAttributeModify);
            //客户端操作缓存
            rel.initTempAttrMap();
        }
        @Override
        public boolean shouldDraw() {
            for (Integer value : rel.attrModifierMap.values()) {
                if (value>0){
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean isEnabled() {
            return shouldDraw();
        }
    }

    public int getModifyAttrTotal(){
        if (attrModifierMap.isEmpty())return 0;
        return attrModifierMap.values().stream().mapToInt(v->v).sum();
    }





}
